/**=========================================================
 * Service: double-select.js
 =========================================================*/

App.service('DoubleSelect', ["$timeout", 'Utils', 'HttpUtils', 'SimpleSelect',
    function($timeout, utils, httpUtils, simpleSelectService) {
        'use strict';
        
        return {
            init: function($scope, options) {
                var defaultOptions = {
                    /**
                     * $scope.selects对象中的属性名
                     */
                    optionModel: '',
                    /**
                     * 后台的查询参数
                     */
                    params: '',
                    /**
                     * 是否请求后台
                     */
                    remote: true,
                    /**
                     * 本地数据
                     */
                    datas: []
                }
                
                var finalOptions = angular.extend(defaultOptions,options);
                
                if (utils.isEmpty($scope.doubleSelects)) {
                    $scope.doubleSelects = {};
                }
                
                $scope.doubleSelects[finalOptions.optionModel] = {};
                
                var simpleSelect = simpleSelectService.init($scope,
                    {
                        optionModel: finalOptions.optionModel,
                        multiple: true,
                        params: finalOptions.params,
                        remote: finalOptions.remote,
                        datas: finalOptions.datas
                    }
                );
                
                $scope.$watch('doubleSelects.' + finalOptions.optionModel + '.selected1', 
                    function (newVal, oldVal) {
                        if (newVal != undefined){
                            $timeout(function(){
                                simpleSelect.clearFilter();
                                if (utils.isEmpty($scope.doubleSelects[finalOptions.optionModel].selected2)){
                                    simpleSelect.filter('value',newVal,'!=');
                                }else{                   
                                    simpleSelect.filter('value',utils.mergeArray(newVal,$scope.doubleSelects[finalOptions.optionModel].selected2),'!=');
                                }
                            });
                        }
                    }
                );
                
                $scope.$watch('doubleSelects.' + finalOptions.optionModel + '.selected2', 
                    function (newVal, oldVal) {
                        if (newVal != undefined){
                            $timeout(function(){
                                simpleSelect.clearFilter();
                                if (utils.isEmpty($scope.doubleSelects[finalOptions.optionModel].selected1)){
                                    simpleSelect.filter('value',newVal,'!=');
                                }else{                   
                                    simpleSelect.filter('value',utils.mergeArray(newVal,$scope.doubleSelects[finalOptions.optionModel].selected1),'!=');
                                }
                            });
                        }    
                    }
                );
                
                return {
                    scope: $scope,
                    options: finalOptions,
                    simpleSelect: simpleSelect,
                    /**
                     * 获取选中值
                     */
                    getSelected: function(){
                        return [this.scope.doubleSelects[this.options.optionModel].selected1,this.scope.doubleSelects[this.options.optionModel].selected2];
                    },
                    /**
                     * 选中指定值
                     * index:1或2
                     */
                    select: function(newVal,index){
                        this.simpleSelect.clearFilter();
                        this.scope.doubleSelects[this.options.optionModel]["selected" + index] = [newVal];
                    },
                    clear: function(index){
                        this.scope.doubleSelects[this.options.optionModel]["selected" + index] = [];
                    }
                };
            }
        }
    }
]);
